package com.dingreading.cloud.admin.util.bean;

import com.dingreading.cloud.admin.dto.MaxFullNameDto;
import com.dingreading.cloud.admin.entity.Members;
import com.dingreading.cloud.admin.service.MembersService;
import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.common.util.TextUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class MemberUtil {

    public static MemberUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    @Resource
    private MembersService membersService;


    public static final List<String> NORMAN_STATUS = new ArrayList<>(Arrays.asList("1", "2", "5"));


    /**
     * 导入时创建会员
     */
    public static Members createMemberByImport(
            String corpUid, String storeUid, String storeName, Long storesId,
            String memberUid, String mob, String name, String grade,String source, String staffUid, String staffName
    ) {
        Members data = new Members();
        data.setMob(mob);
        data.setFullName(name);
        int gradeValue = 0;
        // 幼儿园：小:-3，中:-2，大-1；小学：一:1，二:2，三:3，四:4，五:5，六:6；初中：初一:7，初二:8；未知:0
        if (grade.equals("大班"))
            gradeValue = -1;
        else if (grade.equals("中班"))
            gradeValue = -2;
        else if (grade.equals("小班"))
            gradeValue = -3;
        else if (grade.equals("一年级"))
            gradeValue = 1;
        else if (grade.equals("二年级"))
            gradeValue = 2;
        else if (grade.equals("三年级"))
            gradeValue = 3;
        else if (grade.equals("四年级"))
            gradeValue = 4;
        else if (grade.equals("五年级"))
            gradeValue = 5;
        else if (grade.equals("六年级"))
            gradeValue = 6;
        else if (grade.equals("初一"))
            gradeValue = 7;
        else if (grade.equals("初二"))
            gradeValue = 8;
        else
            gradeValue = 0;
        data.setGrade(gradeValue);

        data.setUid(memberUid);
        // 设置会员系统名称
        data.setMemberName(MemberUtil.getMemberName(data.getFullName()));
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreId(storesId);
        data.setStoreName(storeName);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setSex(0);
        data.setReadBooks(0);
        data.setReadDays(0);
        data.setReadWords(0d);
        data.setEnabled(1);
        data.setFaceId("");
        data.setFaceUpStatus(-1);
        data.setFaceImg("");
        data.setSource(source);
        data.setValidStatus(1);

        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);

        return data;
    }

    /**
     * 设置系统名称，不重复，末尾数字累加，如：李明2，逢4跳过
     *
     * @param fullName 会员集合
     */
    public static String getMemberName(String fullName) {
        MaxFullNameDto maxFullName = util.membersService.maxFullName(fullName);
        if (maxFullName != null) {
            int maxNumber = NepUtil.nullToZero(maxFullName.getMaxNumber());
            if (maxNumber != 0) {
                maxNumber += 1;
            } else {
                maxNumber += 2;
            }
            // 数字最后一位是4则跳过
            maxNumber = TextUtils.dealLastFour(maxNumber);
            fullName = fullName.concat(String.valueOf(maxNumber));
        }
        return fullName;
    }

}